package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.ui.ImagePreviewDelActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.VideoPlayActivity;
import patrol.mobile.com.mobilepatrol.adapter.GlideImageLoader;
import patrol.mobile.com.mobilepatrol.adapter.ImagePickerAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.DeviceDetailImgBean;
import patrol.mobile.com.mobilepatrol.bean.GzbsbBean;
import patrol.mobile.com.mobilepatrol.bean.UpImgInfoBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.picker.matisse.Matisse;
import patrol.mobile.com.mobilepatrol.picker.matisse.MimeType;
import patrol.mobile.com.mobilepatrol.picker.matisse.engine.GlideEngine;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil02;
import patrol.mobile.com.mobilepatrol.utils.FileUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.LuBanHelper;
import patrol.mobile.com.mobilepatrol.utils.SDFileHelper;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.dialog.SelectDialog;

import static patrol.mobile.com.mobilepatrol.activity.office.AdviceDetailActivity.IMAGE_ITEM_ADD;
import static patrol.mobile.com.mobilepatrol.utils.SPUtils.get;

/**
 * Created by z on 2018/9/20.
 * Des: 故障报送表
 */

public class GzbsbActivity extends BaseActivity implements ImagePickerAdapter.OnRecyclerViewItemClickListener {
    private static final String TAG = "GZZZZZZZZ";
    @BindView(R.id.mEditBsdw)
    EditText mEditBsdw;
    @BindView(R.id.mEditBsr)
    EditText mEditBsr;
    @BindView(R.id.mEditSbmc)
    EditText mEditSbmc;
    @BindView(R.id.mEditGzxx)
    EditText mEditGzxx;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.mEditBsRq)
    TextView mEditBsRq;
    //    @BindView(R.id.lbImage01)
//    ImageView lbImage01;
//    @BindView(R.id.lb01)
//    LinearLayout lb01;
//    @BindView(R.id.lbImage02)
//    ImageView lbImage02;
//    @BindView(R.id.lb02)
//    LinearLayout lb02;
//    @BindView(R.id.lbImage03)
//    ImageView lbImage03;
//    @BindView(R.id.lb03)
//    LinearLayout lb03;
//    @BindView(R.id.lbImage04)
//    ImageView lbImage04;
//    @BindView(R.id.lb04)
//    LinearLayout lb04;
//    @BindView(R.id.lbImage05)
//    ImageView lbImage05;
//    @BindView(R.id.lb05)
//    LinearLayout lb05;
//    @BindView(R.id.lbImage06)
//    ImageView lbImage06;
//    @BindView(R.id.lb06)
//    LinearLayout lb06;
//    @BindView(R.id.yyImage01)
//    ImageView yyImage01;
//    @BindView(R.id.yy01)
//    LinearLayout yy01;
//    @BindView(R.id.yyImage02)
//    ImageView yyImage02;
//    @BindView(R.id.yy02)
//    LinearLayout yy02;
//    @BindView(R.id.yyImage03)
//    ImageView yyImage03;
//    @BindView(R.id.yy03)
//    LinearLayout yy03;
//    @BindView(R.id.yyImage04)
//    ImageView yyImage04;
//    @BindView(R.id.yy04)
//    LinearLayout yy04;
//    @BindView(R.id.yyImage05)
//    ImageView yyImage05;
//    @BindView(R.id.yy05)
//    LinearLayout yy05;
    @BindView(R.id.rb_lb01)
    RadioButton rb_lb01;
    @BindView(R.id.rb_lb02)
    RadioButton rb_lb02;
    @BindView(R.id.rb_lb03)
    RadioButton rb_lb03;
    @BindView(R.id.rb_lb04)
    RadioButton rb_lb04;
    @BindView(R.id.rb_lb05)
    RadioButton rb_lb05;
    @BindView(R.id.rb_lb06)
    RadioButton rb_lb06;

    @BindView(R.id.rb_yy01)
    RadioButton rb_yy1;
    @BindView(R.id.rb_yy02)
    RadioButton rb_yy2;
    @BindView(R.id.rb_yy03)
    RadioButton rb_yy3;
    @BindView(R.id.rb_yy04)
    RadioButton rb_yy4;
    @BindView(R.id.rb_yy05)
    RadioButton rb_yy5;

    @BindView(R.id.tv_submit)
    TextView tv_submit;

    private ArrayList<ImageItem> selImageList;
    private ImagePickerAdapter adapter;
    private int limitSize = 4;
    private String failureType = "";// 故障类型  1:收费 2:通信 3:供电 4:照明 5:通风 6:监控
    List<String> failureTypes;
    private String failureYy = "";
    List<String> failureYys;
    private String id;
    private List<String> imgUUIDs = new ArrayList<>();
    private String dataId;
    ArrayList<ImageItem> images = null;

    private List<ImageView> imageViewTypes = new ArrayList<>();
    private List<ImageView> imageViewsYy = new ArrayList<>();
    public static final int REQUEST_CODE_SELECT = 100;
    public static final int REQUEST_CODE_VSELECT = 102;
    public static final int REQUEST_CODE_PREVIEW = 101;
    private String videoPath = "";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_gzbsb_layout;
    }


    @Override
    public void initTitle() {
        super.initTitle();
        dataId = getIntent().getStringExtra("data");
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mEditBsRq.setText(TimeUtils.dateString4(System.currentTimeMillis()));
//        imageViewsYy.add(yyImage01);
//        imageViewsYy.add(yyImage02);
//        imageViewsYy.add(yyImage03);
//        imageViewsYy.add(yyImage04);
//        imageViewsYy.add(yyImage05);
//        imageViewTypes.add(lbImage01);
//        imageViewTypes.add(lbImage02);
//        imageViewTypes.add(lbImage03);
//        imageViewTypes.add(lbImage04);
//        imageViewTypes.add(lbImage05);
//        imageViewTypes.add(lbImage06);
        if (dataId == null) {
            tv_center_title.setText("故障报修编辑");
            mEditBsr.setText(UserManager.getryName());
            mEditBsr.setEnabled(false);
            mEditBsdw.setText(UserManager.getUser((String) get(Constants.current_id, "")).bmname);
            mEditBsdw.setEnabled(false);
            setMySpinner();
        } else {
            tv_center_title.setText("故障报送表");
//            lbImage01.setImageResource(R.mipmap.radio);
//            yyImage01.setImageResource(R.mipmap.radio);

            tv_submit.setVisibility(View.GONE);
            setData();
        }
    }

    boolean bRbLb01 = false,
            bRbLb02 = false,
            bRbLb03 = false,
            bRbLb04 = false,
            bRbLb05 = false,
            bRbLb06 = false,
            bRbYy01 = false,
            bRbYy02 = false,
            bRbYy03 = false,
            bRbYy04 = false,
            bRbYy05 = false;

    private void setMySpinner() {
        // 故障类型  1:收费 2:通信 3:供电 4:照明 5:通风 6:监控
        failureTypes = new ArrayList<>();
        failureTypes.add("收费");
        failureTypes.add("通信");
        failureTypes.add("供电");
        failureTypes.add("照明");
        failureTypes.add("通风");
        failureTypes.add("监控");
        nuViewRb(true);
        rb_lb01.setOnClickListener(v -> {
            bRbLb01 = !bRbLb01;
            rb_lb01.setChecked(bRbLb01);
        });
        rb_lb02.setOnClickListener(v -> {
            bRbLb02 = !bRbLb02;
            rb_lb02.setChecked(bRbLb02);
        });
        rb_lb03.setOnClickListener(v -> {
            bRbLb03 = !bRbLb03;
            rb_lb03.setChecked(bRbLb03);
        });
        rb_lb04.setOnClickListener(v -> {
            bRbLb04 = !bRbLb04;
            rb_lb04.setChecked(bRbLb04);
        });
        rb_lb05.setOnClickListener(v -> {
            bRbLb05 = !bRbLb05;
            rb_lb05.setChecked(bRbLb05);
        });
        rb_lb06.setOnClickListener(v -> {
            bRbLb06 = !bRbLb06;
            rb_lb06.setChecked(bRbLb06);
        });
        rb_yy1.setOnClickListener(v -> {
            bRbYy01 = !bRbYy01;
            rb_yy1.setChecked(bRbYy01);
        });
        rb_yy2.setOnClickListener(v -> {
            bRbYy02 = !bRbYy02;
            rb_yy2.setChecked(bRbYy02);
        });
        rb_yy3.setOnClickListener(v -> {
            bRbYy03 = !bRbYy03;
            rb_yy3.setChecked(bRbYy03);
        });
        rb_yy4.setOnClickListener(v -> {
            bRbYy04 = !bRbYy04;
            rb_yy4.setChecked(bRbYy04);
        });
        rb_yy5.setOnClickListener(v -> {
            bRbYy05 = !bRbYy05;
            rb_yy5.setChecked(bRbYy05);
        });
//        failureType = "1";
//        lb01.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                failureType = "1";
////                setLbImage(imageViewTypes.get(0));
//                if (TextUtils.isEmpty(failureType)) {
//                    failureType = failureTypes.get(0) + ",";
//                    imageViewTypes.get(0).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureType.contains(failureTypes.get(0))) {
//                        failureType.replace(failureTypes.get(0) + ",", "");
//                        imageViewTypes.get(0).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureType = failureType + failureTypes.get(0) + ",";
//                        imageViewTypes.get(0).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        lb02.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureType)) {
//                    failureType = failureTypes.get(1) + ",";
//                    imageViewTypes.get(1).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureType.contains(failureTypes.get(1))) {
//                        failureType.replace(failureTypes.get(1) + ",", "");
//                        imageViewTypes.get(1).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureType = failureType + failureTypes.get(1) + ",";
//                        imageViewTypes.get(1).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        lb03.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureType)) {
//                    failureType = failureTypes.get(2) + ",";
//                    imageViewTypes.get(2).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureType.contains(failureTypes.get(2))) {
//                        failureType.replace(failureTypes.get(2) + ",", "");
//                        imageViewTypes.get(2).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureType = failureType + failureTypes.get(2) + ",";
//                        imageViewTypes.get(2).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        lb04.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureType)) {
//                    failureType = failureTypes.get(3) + ",";
//                    imageViewTypes.get(3).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureType.contains(failureTypes.get(3))) {
//                        failureType.replace(failureTypes.get(3) + ",", "");
//                        imageViewTypes.get(3).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureType = failureType + failureTypes.get(3) + ",";
//                        imageViewTypes.get(3).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        lb05.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureType)) {
//                    failureType = failureTypes.get(4) + ",";
//                    imageViewTypes.get(4).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureType.contains(failureTypes.get(4))) {
//                        failureType.replace(failureTypes.get(4) + ",", "");
//                        imageViewTypes.get(4).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureType = failureType + failureTypes.get(4) + ",";
//                        imageViewTypes.get(4).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        lb06.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureType)) {
//                    failureType = failureTypes.get(5) + ",";
//                    imageViewTypes.get(5).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureType.contains(failureTypes.get(5))) {
//                        failureType.replace(failureTypes.get(5) + ",", "");
//                        imageViewTypes.get(5).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureType = failureType + failureTypes.get(5) + ",";
//                        imageViewTypes.get(5).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//
//            }
//        });
        failureYys = new ArrayList<>();
        failureYys.add("线路");
        failureYys.add("供电");
        failureYys.add("硬件");
        failureYys.add("软件");
        failureYys.add("其他");
//        yy01.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureYy)) {
//                    failureYy = failureYys.get(0) + ",";
//                    imageViewsYy.get(0).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureYy.contains(failureYys.get(0))) {
//                        failureYy.replace(failureYys.get(0) + ",", "");
//                        imageViewsYy.get(0).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureYy = failureYy + failureYys.get(0) + ",";
//                        imageViewsYy.get(0).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        yy02.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureYy)) {
//                    failureYy = failureYys.get(1) + ",";
//                    imageViewsYy.get(1).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureYy.contains(failureYys.get(1))) {
//                        failureYy.replace(failureYys.get(1) + ",", "");
//                        imageViewsYy.get(1).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureYy = failureYy + failureYys.get(1) + ",";
//                        imageViewsYy.get(1).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        yy03.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureYy)) {
//                    failureYy = failureYys.get(2) + ",";
//                    imageViewsYy.get(2).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureYy.contains(failureYys.get(2))) {
//                        failureYy.replace(failureYys.get(2) + ",", "");
//                        imageViewsYy.get(2).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureYy = failureYy + failureYys.get(2) + ",";
//                    if (failureYy.contains(failureYys.get(3))) {
//                        imageViewsYy.get(2).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        yy04.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureYy)) {
//                    failureYy = failureYys.get(3) + ",";
//                    imageViewsYy.get(3).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                        failureYy.replace(failureYys.get(3) + ",", "");
//                        imageViewsYy.get(3).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureYy = failureYy + failureYys.get(3) + ",";
//                        imageViewsYy.get(3).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });
//        yy05.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty(failureYy)) {
//                    failureYy = failureYys.get(4) + ",";
//                    imageViewsYy.get(4).setImageResource(R.mipmap.radio_button_on);
//                } else {
//                    if (failureYy.contains(failureYys.get(4))) {
//                        failureYy.replace(failureYys.get(4) + ",", "");
//                        imageViewsYy.get(4).setImageResource(R.mipmap.radio);
//                    } else {
//                        failureYy = failureYy + failureYys.get(4) + ",";
//                        imageViewsYy.get(4).setImageResource(R.mipmap.radio_button_on);
//                    }
//                }
//            }
//        });

        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tijiaoData();
            }
        });
        mEditBsRq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerUtil02.getInstance(v.getContext()).setCallBack(new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        mEditBsRq.setText(dateBean.getmDate());
                    }
                });
            }
        });
    }

    private void setLbImage(ImageView image) {
//        for (ImageView imageViewType : imageViewTypes) {
//            imageViewType.setImageResource(R.mipmap.danxuan);
//        }
        image.setImageResource(R.mipmap.radio_button_on);
    }

    private void setLbImage(String image) {
        //故障类型  1:收费 2:通信 3:供电 4:照明 5:通风 6:监控
//        for (ImageView imageViewType : imageViewTypes) {
//            imageViewType.setImageResource(R.mipmap.danxuan);
//        }
        switch (image) {
            case "收费":
                bRbLb01 = true;
                rb_lb01.setChecked(true);
//                imageViewTypes.get(0).setImageResource(R.mipmap.radio_button_on);
                break;
            case "通信":
//                imageViewTypes.get(1).setImageResource(R.mipmap.radio_button_on);
                bRbLb02 = true;
                rb_lb02.setChecked(true);
                break;
            case "供电":
//                imageViewTypes.get(2).setImageResource(R.mipmap.radio_button_on);
                bRbLb03 = true;
                rb_lb03.setChecked(true);
                break;
            case "照明":
//                imageViewTypes.get(3).setImageResource(R.mipmap.radio_button_on);
                bRbLb04 = true;
                rb_lb04.setChecked(true);
                break;
            case "通风":
//                imageViewTypes.get(4).setImageResource(R.mipmap.radio_button_on);
                bRbLb05 = true;
                rb_lb05.setChecked(true);
                break;
            case "监控":
//                imageViewTypes.get(5).setImageResource(R.mipmap.radio_button_on);
                bRbLb06 = true;
                rb_lb06.setChecked(true);
                break;
//            default:
//                imageViewTypes.get(0).setImageResource(R.mipmap.radio_button_on);
//                break;
        }

    }

    private void setYyImage(ImageView image) {
        for (ImageView imageViewType : imageViewsYy) {
            imageViewType.setImageResource(R.mipmap.danxuan);
        }
        image.setImageResource(R.mipmap.radio_button_on);
    }

    private void setYyImage(String image) {
//        for (ImageView imageViewType : imageViewsYy) {
//            imageViewType.setImageResource(R.mipmap.danxuan);
//        }
        switch (image) {
            case "线路":
                bRbYy01 = true;
                rb_yy1.setClickable(true);
//                imageViewsYy.get(0).setImageResource(R.mipmap.radio_button_on);
                break;
            case "供电":
                bRbYy02 = true;
                rb_yy2.setClickable(true);
//                imageViewsYy.get(1).setImageResource(R.mipmap.radio_button_on);
                break;
            case "硬件":
                bRbYy03 = true;
                rb_yy3.setClickable(true);
//                imageViewsYy.get(2).setImageResource(R.mipmap.radio_button_on);
                break;
            case "软件":
                bRbYy04 = true;
                rb_yy4.setClickable(true);
//                imageViewsYy.get(3).setImageResource(R.mipmap.radio_button_on);
                break;
            case "其他":
                bRbYy05 = true;
                rb_yy5.setClickable(true);
//                imageViewsYy.get(4).setImageResource(R.mipmap.radio_button_on);
                break;
//            default:
//                imageViewsYy.get(0).setImageResource(R.mipmap.radio_button_on);
//                break;
        }
    }

    @Override
    public void setListener() {
    }

    private void tijiaoData() {
        if (greadData() == null) {
            return;
        }
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("mainJson", new Gson().toJson(greadData()));
        List<ImageItem> images = adapter.getImages();
        List<UpImgInfoBean> upImgInfoBeanList = new ArrayList<>();
        for (ImageItem image : images) {
            UpImgInfoBean imgInfoBean = new UpImgInfoBean();
            imgInfoBean.setBusniessId(id);
            imgInfoBean.setContentType("jiankkElefailrpt");
            imgInfoBean.setFileType("img");
            String uuid = UUID.randomUUID().toString();
            imgUUIDs.add(uuid);
            imgInfoBean.setId(uuid);
            upImgInfoBeanList.add(imgInfoBean);
        }
        stringStringHashMap.put("sysAttachmentsJson", GsonUtil.bean2Json(upImgInfoBeanList));
        HttpSender sender = new HttpSender(this, baseUrl, Urls.jiankkElefailrptUploadData);
        sender.requestPost(stringStringHashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("返回数据", response);
                upImg();
            }
        });


    }

    private void upImg() {
        if (selImageList != null && selImageList.size() != 0) {
            List<File> list = new ArrayList<>();
            List<Map<String, String>> params = new ArrayList<>();
            List<Map<String, String>> picparams = new ArrayList<>();
            for (String imgUUID : imgUUIDs) {
                Map<String, String> map = new HashMap<>();
                map.put("id", imgUUID);
                map.put("type", "jiankkElefailrpt");
                params.add(map);
            }
            for (int i = 0; i < selImageList.size(); i++) {
                File file;
                File videofile;
                if (selImageList.get(i).mimeType.equals("image")) {
                    file = new File(selImageList.get(i).path);
                    list.add(file);
                    picparams.add(params.get(i));
                } else {
                    videofile = new File(selImageList.get(i).name);
                    HttpSender sender = new HttpSender(this, baseUrl, Urls.IMG_UP_LOAD);
                    Map<String, String> map = new HashMap<>();
                    map.put("id", imgUUIDs.get(i));
                    map.put("type", "jiankkElefailrpt");
                    int finalI = i;
                    sender.upImgs2(videofile, map, new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.i(TAG, "videoonResponse: " + e);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.i(TAG, "videoonResponse: " + response);
                            Toast.makeText(GzbsbActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                            if (finalI == selImageList.size() - 1) {
                                finish();
                            }
                        }
                    });
                }
            }

            LuBanHelper luBanHelper = new LuBanHelper(GzbsbActivity.this, list, picparams);
            luBanHelper.compression(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Log.i(TAG, "onResponse: " + e);
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.i(TAG, "onResponse: " + response);
                    Toast.makeText(GzbsbActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
        }
    }


    private HashMap<String, Object> greadData() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("orgId", UserManager.getOrgNo());
        hashMap.put("account", UserManager.getUserId());
        id = UserManager.getUserId() + System.currentTimeMillis();
        hashMap.put("id", id);
        HashMap<String, String> hashMap1 = new HashMap<>();
        hashMap1.put("id", UserManager.getOrgNo());
        hashMap1.put("orgName", UserManager.getUser((String) get(Constants.current_id, "")).bmname);
        hashMap.put("org", hashMap1);
        if (TextUtils.isEmpty(mEditBsRq.getText())) {
            ToastUtils.showToast("请选择报送日期");
            return null;
        } else {
            hashMap.put("date", TimeUtils.timeToData(mEditBsRq.getText().toString()));
        }

        if (TextUtils.isEmpty(mEditSbmc.getText())) {
            ToastUtils.showToast("请输入设备名称");
            return null;
        } else {
            hashMap.put("equipmentName", mEditSbmc.getText().toString());
        }

        if (TextUtils.isEmpty(mEditGzxx.getText())) {
            ToastUtils.showToast("请输入故障现象");
            return null;
        } else {
            hashMap.put("failureContent", mEditGzxx.getText().toString());
        }
        if (selImageList == null || selImageList.size() == 0) {
            ToastUtils.showToast("请上传图片");
            return null;
        }
        if (rb_lb01.isChecked()) {
            failureType = failureTypes.get(0);
        }
        if (rb_lb02.isChecked()) {
            if (failureType.length() > 0) {
                failureType.concat(",");
            }
            failureType.concat(failureTypes.get(1));
        }
        if (rb_lb03.isChecked()) {
            if (failureType.length() > 0) {
                failureType.concat(",");
            }
            failureType.concat(failureTypes.get(2));
        }
        if (rb_lb04.isChecked()) {
            if (failureType.length() > 0) {
                failureType.concat(",");
            }
            failureType.concat(failureTypes.get(3));
        }
        if (rb_lb05.isChecked()) {
            if (failureType.length() > 0) {
                failureType.concat(",");
            }
            failureType.concat(failureTypes.get(4));
        }
        if (rb_lb06.isChecked()) {
            if (failureType.length() > 0) {
                failureType.concat(",");
            }
            failureType.concat(failureTypes.get(5));
        }

        if (rb_yy1.isChecked()) {
            failureYy = failureYys.get(0);
        }
        if (rb_yy2.isChecked()) {
            if (failureYy.length() > 0) {
                failureYy.concat(",");
            }
            failureYy.concat(failureYys.get(1));
        }
        if (rb_yy3.isChecked()) {
            if (failureYy.length() > 0) {
                failureYy.concat(",");
            }
            failureYy.concat(failureYys.get(2));
        }
        if (rb_yy4.isChecked()) {
            if (failureYy.length() > 0) {
                failureYy.concat(",");
            }
            failureYy.concat(failureYys.get(3));
        }
        if (rb_yy5.isChecked()) {
            if (failureYy.length() > 0) {
                failureYy.concat(",");
            }
            failureYy.concat(failureYys.get(4));
        }

        hashMap.put("failureReason", failureYy);
        hashMap.put("failureType", failureType);
        return hashMap;
    }


    @Override
    public void initData() {
        super.initData();
        initImagePicker();
        selImageList = new ArrayList<>();
        adapter = new ImagePickerAdapter(this, selImageList, limitSize);
        adapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(this, limitSize));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
    }

    private void setData() {
        nuView(false);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getJiankkElefailrptEntity);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", dataId);
        sender.requestPost(map, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                GzbsbBean gzbsbBean = GsonUtil.GsonToBean(response, GzbsbBean.class);
                mEditBsRq.setText(gzbsbBean.getCreateDate());
                mEditBsdw.setText(gzbsbBean.getOrg().getOrgName());
//                mEditBsr.setText(gzbsbBean.getAccount().getName());
                mEditSbmc.setText(gzbsbBean.getEquipmentName());
                mEditGzxx.setText(gzbsbBean.getFailureContent());
                if (gzbsbBean.getFailureReason().contains(",")) {
                    String[] split = gzbsbBean.getFailureReason().split(",");
                    for (int i = 0; i < split.length; i++) {
                        setYyImage(split[i]);
                    }
                } else {
                    setYyImage(gzbsbBean.getFailureReason());
                }
                if (gzbsbBean.getFailureType().contains(",")) {
                    String[] split = gzbsbBean.getFailureType().split(",");
                    for (int i = 0; i < split.length; i++) {
                        setLbImage(split[i]);
                    }
                } else {
                    setLbImage(gzbsbBean.getFailureType());
                }


                List<DeviceDetailImgBean> deviceDetailImgBeans = GsonUtil.jsonToList(attachList, DeviceDetailImgBean.class);
                images = new ArrayList<>();
                SDFileHelper fileHelper = new SDFileHelper(GzbsbActivity.this);

                for (DeviceDetailImgBean deviceDetailImgBean : deviceDetailImgBeans) {
                    String url = baseUrl + deviceDetailImgBean.getFilePath();
                    String replace = url.replace("\\", "/");
                    if (deviceDetailImgBean.getFileType().equals("mp4")) {
                        //获取第一帧图像的bitmap对象
                        Bitmap bitmap = getNetVideoBitmap(replace);
                        String imgUrl = FileUtil.saveImg2Gallery(GzbsbActivity.this, bitmap,
                                System.currentTimeMillis() / 1000 + "");
                        ImageItem imageItem = new ImageItem();
                        UpImgInfoBean upImgInfoBean = new UpImgInfoBean();
                        imageItem.path = imgUrl;
                        imageItem.name = replace;
                        imageItem.mimeType = "video";
                        selImageList.add(imageItem);
                        upImgInfoBean.setId(deviceDetailImgBean.getId());
                        upImgInfoBean.setFileType(deviceDetailImgBean.getFileType());
                        upImgInfoBean.setContentType(deviceDetailImgBean.getContentType());
                        upImgInfoBean.setBusniessId(deviceDetailImgBean.getBusniessId());
                    } else {
                        long currentTime = System.currentTimeMillis();
                        fileHelper.savePicture(currentTime + ".jpg", replace);
                        ImageItem imageItem = new ImageItem();
                        UpImgInfoBean upImgInfoBean = new UpImgInfoBean();
                        try {
                            String filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/budejie/" + currentTime + ".jpg";
                            imageItem.path = filePath;
                            imageItem.mimeType = "image";
                            Log.i(TAG, "onResponse: " + filePath);
                            selImageList.add(imageItem);
                            upImgInfoBean.setId(deviceDetailImgBean.getId());
                            upImgInfoBean.setFileType(deviceDetailImgBean.getFileType());
                            upImgInfoBean.setContentType(deviceDetailImgBean.getContentType());
                            upImgInfoBean.setBusniessId(deviceDetailImgBean.getBusniessId());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                fileHelper.setOnFileSaveSuccessListener(new SDFileHelper.OnFileSaveSuccessListener() {
                    @Override
                    public void onFileSaveSuccess() {
                        adapter.setImages(selImageList);
                    }
                });
            }
        });
    }

    public static Bitmap getNetVideoBitmap(String videoUrl) {
        Bitmap bitmap = null;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根据url获取缩略图
            retriever.setDataSource(videoUrl, new HashMap());
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }

    private void nuViewRb(boolean enabled) {

        rb_lb01.setClickable(enabled);
        rb_lb02.setClickable(enabled);
        rb_lb03.setClickable(enabled);
        rb_lb04.setClickable(enabled);
        rb_lb05.setClickable(enabled);
        rb_lb06.setClickable(enabled);
        rb_yy1.setClickable(enabled);
        rb_yy2.setClickable(enabled);
        rb_yy3.setClickable(enabled);
        rb_yy4.setClickable(enabled);
        rb_yy5.setClickable(enabled);
    }

    private void nuView(boolean enabled) {
        mEditBsdw.setEnabled(enabled);
        mEditBsr.setEnabled(enabled);
        mEditSbmc.setEnabled(enabled);
        mEditGzxx.setEnabled(enabled);
        mEditBsRq.setEnabled(enabled);
        nuViewRb(enabled);
//        lb01.setOnClickListener(null);
//        lb02.setOnClickListener(null);
//        lb03.setOnClickListener(null);
//        lb04.setOnClickListener(null);
//        lb05.setOnClickListener(null);
//        lb06.setOnClickListener(null);
//        yy01.setOnClickListener(null);
//        yy02.setOnClickListener(null);
//        yy03.setOnClickListener(null);
//        yy04.setOnClickListener(null);
//        yy05.setOnClickListener(null);
    }

    private void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(true);                            //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(4);              //选中数量限制
        imagePicker.setMultiMode(false);                      //多选
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (position) {
            case IMAGE_ITEM_ADD:
                if (dataId == null) {
                    List<String> names = new ArrayList<>();
                    names.add("拍照");
                    names.add("相册");
                    names.add("视频");
                    showDialog(new SelectDialog.SelectDialogListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            switch (position) {
                                case 0: // 直接调起相机
                                    /**
                                     * 0.4.7 目前直接调起相机不支持裁剪，如果开启裁剪后不会返回图片，请注意，后续版本会解决
                                     *
                                     * 但是当前直接依赖的版本已经解决，考虑到版本改动很少，所以这次没有上传到远程仓库
                                     *
                                     * 如果实在有所需要，请直接下载源码引用。
                                     */
                                    //打开选择,本次允许选择的数量
                                    ImagePicker.getInstance().setSelectLimit(limitSize - selImageList.size());
                                    Intent intent = new Intent(GzbsbActivity.this, ImageGridActivity.class);
                                    intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                                    startActivityForResult(intent, REQUEST_CODE_SELECT);
                                    break;
                                case 1:
                                    //打开选择,本次允许选择的数量
                                    ImagePicker.getInstance().setSelectLimit(limitSize - selImageList.size());
                                    Intent intent1 = new Intent(GzbsbActivity.this, ImageGridActivity.class);
                                    /* 如果需要进入选择的时候显示已经选中的图片，
                                     * 详情请查看ImagePickerActivity
                                     * */
//                                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
                                    startActivityForResult(intent1, REQUEST_CODE_SELECT);
                                    break;
                                case 2:
//                                Intent i = new Intent();
//                                i.setType("video/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
//                                i.setAction(Intent.ACTION_GET_CONTENT);
//                                startActivityForResult(i, REQUEST_CODE_VSELECT);
                                    Matisse.from(GzbsbActivity.this)
                                            .choose(MimeType.ofVideo())
                                            .showSingleMediaType(true)
                                            .theme(R.style.Matisse_Dracula)
                                            .countable(false)
                                            .maxSelectable(1)
                                            .imageEngine(new GlideEngine())
                                            .forResult(REQUEST_CODE_VSELECT);
                                    break;
                                default:
                                    break;
                            }

                        }
                    }, names);
                }
                break;
            default:
                if (adapter.getImages().get(position).mimeType.equals("image")) {
                    //打开预览
                    Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);
                    intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>) adapter.getImages());
                    intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                    intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);
                    startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                } else {
                    Intent intentPreview = new Intent(this, VideoPlayActivity.class);
                    intentPreview.putExtra("videopath", adapter.getImages().get(position).name);
                    startActivity(intentPreview);
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            switch (resultCode) {
                case ImagePicker.RESULT_CODE_ITEMS:
                    //添加图片返回
                    if (requestCode == REQUEST_CODE_SELECT) {
                        images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        if (images != null) {
                            for (int i = 0; i < images.size(); i++) {
                                images.get(i).mimeType = "image";
                            }
                            selImageList.addAll(images);
                            adapter.setImages(selImageList);
                        }
                    }
                    break;
                case ImagePicker.RESULT_CODE_BACK:
                    //预览图片返回
                    if (requestCode == REQUEST_CODE_PREVIEW) {
                        images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
                        if (images != null) {
                            selImageList.clear();
                            selImageList.addAll(images);
                            adapter.setImages(selImageList);
                        }
                    }
                    break;
                case RESULT_OK:
                    //选择视频返回
                    if (requestCode == REQUEST_CODE_VSELECT) {
                        List<String> videoUrls = Matisse.obtainPathResult(data);
                        Log.e("videoUrls", videoUrls.get(0));
                        videoPath = videoUrls.get(0);
                        MediaMetadataRetriever retr = new MediaMetadataRetriever();
                        retr.setDataSource(videoPath);
                        //获取第一帧图像的bitmap对象
                        Bitmap bitmap = retr.getFrameAtTime();
                        String imgUrl = FileUtil.saveImg2Gallery(this, bitmap,
                                System.currentTimeMillis() / 1000 + "");
                        ImageItem imageItem = new ImageItem();
                        imageItem.path = imgUrl;
                        imageItem.name = videoPath;
                        imageItem.mimeType = "video";
                        selImageList.add(imageItem);
                        adapter.setImages(selImageList);

                    }
                    break;
            }
        }
    }

    private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, List<String> names) {
        SelectDialog dialog = new SelectDialog(this, R.style
                .transparentFrameWindowStyle,
                listener, names);
        if (!this.isFinishing()) {
            dialog.show();
        }
        return dialog;
    }


}
